package com.example.market;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class FragmentUpdate extends Fragment {

    private List<ImageItem> imageItems = new ArrayList<>();
    ListView lv_update;

    private UpdateAdapter adapter;
    private ProgressBar progressBar;

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        lv_update =view. findViewById(R.id.lv_update);
        progressBar =view. findViewById(R.id.progressBar);

        imageItems.add(new ImageItem(R.drawable.weather, "天气"));
        imageItems.add(new ImageItem(R.drawable.market, "应用市场"));

        imageItems.add(new ImageItem(R.drawable.air, "空调"));
        imageItems.add(new ImageItem(R.drawable.music, "音乐播放"));
        imageItems.add(new ImageItem(R.drawable.user, "用户中心"));
        imageItems.add(new ImageItem(R.drawable.drive, "疲劳驾驶"));
        imageItems.add(new ImageItem(R.drawable.quanjing, "360全景"));


        adapter =new  UpdateAdapter();


        lv_update.setAdapter(adapter);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.activity_update, container, false);
    }

    private class UpdateAdapter extends BaseAdapter {



        @Override
        public int getCount() {
            return imageItems.size();
        }

        @Override
        public Object getItem(int position) {

            return imageItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {

            view = LayoutInflater.from(getContext()).inflate(R.layout.update, viewGroup, false);

            ImageView imgLogo = view.findViewById(R.id.imgLogo);
            TextView txt_name = view.findViewById(R.id.txt_name);
            Button update = view.findViewById(R.id.btn_update);


            ImageItem item = imageItems.get(position);
            imgLogo.setImageResource(item.getImageView());
            txt_name.setText(item.getTextname());


            update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    progressBar.setVisibility(View.VISIBLE); // 显示进度条
                    Toast.makeText(getContext(), "正在更新...", Toast.LENGTH_SHORT).show();
                    new DownloadTask().execute();

                }
            });

            return view;
        }


        class DownloadTask extends AsyncTask<Void, Integer, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }


            @Override
            protected Void doInBackground(Void... voids) {
                for (int i = 0; i < 100; i++) {
                    try {
                        Thread.sleep(100);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    publishProgress(i);

                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                progressBar.setProgress(values[0]);
            }

            @Override
            protected void onPostExecute(Void unused) {
                super.onPostExecute(unused);

                Toast.makeText(getContext(), "更新完成", Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.GONE);
            }
        }
    }
}